package com.miao.avchat
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.tabBarClasses.Tab;
	import mx.core.*;

	use namespace mx_internal;
	
	public class RichTab extends Tab
	{
		public static const CLOSE_TAB:String="Close_Tab";
		private var btn_Close:Button;
		
		[Embed(source="images/close.png")] 
		private var closeIcon:Class; 
		
		public function RichTab()
		{
			super();
			this.mouseChildren=true;
		}
		
		override protected function createChildren():void{
			
			super.createChildren();
			this.btn_Close=new Button();
			this.btn_Close.width=10;
			this.btn_Close.height = 10;
			this.btn_Close.toolTip="关闭";
			this.btn_Close.addEventListener(MouseEvent.CLICK,onClickHandler);
			this.btn_Close.setStyle("icon" , closeIcon);
			if("消息中心" == label) {
				this.btn_Close.width=0;
				this.btn_Close.height=0;
				this.btn_Close.setStyle("icon" , null);
			}
			
			this.addChild(this.btn_Close);
			this.width = 150;
			
			this.btn_Close.addEventListener(MouseEvent.MOUSE_OVER , visableCloseBtn);
			this.btn_Close.addEventListener(MouseEvent.MOUSE_OUT , visableCloseBtn);
			this.addEventListener(MouseEvent.MOUSE_OVER , visableCloseBtn);
			this.addEventListener(MouseEvent.MOUSE_OUT , visableCloseBtn);
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			this.setChildIndex(this.btn_Close,this.numChildren-1);
			if(this.selected){
				this.btn_Close.visible=true;
				this.btn_Close.enabled=true;
			}else{
				this.btn_Close.visible=false;
				this.btn_Close.enabled=false;
			}
			
			this.btn_Close.x=unscaledWidth-this.btn_Close.width-4;
			this.btn_Close.y=4;
		}
		
		private function onClickHandler(event:MouseEvent):void{
			this.dispatchEvent(new MouseEvent(RichTab.CLOSE_TAB));
			event.stopImmediatePropagation();
		}
		
		private function visableCloseBtn(event:MouseEvent) : void {
			if(this.selected)
				return ;
			
			switch(event.type) {
				case MouseEvent.MOUSE_OVER : 
					this.btn_Close.visible=true;
					this.btn_Close.enabled=true;
					break;
				
				case MouseEvent.MOUSE_OUT :
					this.btn_Close.visible=false;
					this.btn_Close.enabled=false;
					break;
				
				default :
					
					break;
				
			}
			
		}
	}
}
